package main

import (
	"flag"
	"fmt"
	"runtime"
	"study-crontab/crontab/worker"
	"time"
)

var (
	confFile string //配置文件路径
)

//解析命令行参数
func initArgs() {
	flag.StringVar(&confFile, "config", "worker.json", "指定worker.json")
	flag.Parse()
}

func initEnv() {
	//线程数与核心数相等
	runtime.GOMAXPROCS(runtime.NumCPU())
}

func main() {
	initArgs()

	//初始化线程
	initEnv()

	//加载配置
	err := worker.InitConfig(confFile)
	// 服务注册
	err = worker.NewRegister()
	if err != nil {
		return
	}
	//启动日志协程
	err = worker.InitLogSink()
	if err != nil {
		return
	}

	//启动执行器
	err = worker.NewExecutor()
	if err != nil {
		return
	}

	//启动调度器
	err = worker.NewScheduler()
	if err != nil {
		fmt.Println(err)
		return
	}

	err = worker.NewJobMgr()
	if err != nil {
		return
	}

	for {
		time.Sleep(1 * time.Second)
	}
}
